/**
 * FileName: AddressServiceImpl
 * Author:   sky
 * Date:     2020/1/3 16:18
 * Description:
 */
package com.run.guifeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.run.guifeng.dao.AddressMapper;
import com.run.guifeng.model.entity.Address;
import com.run.guifeng.model.enums.ServiceResultEnum;
import com.run.guifeng.service.AddressService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 *
 *
 * @author sky
 * @create 2020/1/3
 * @since 1.0.0
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Resource
    AddressMapper addressMapper;

    @Override
    public String saveAddress(Address address) {
        if (addressMapper.insert(address) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String   deleteAddress(Integer id) {

        if (addressMapper.deleteById(id) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DATA_NOT_EXIST.getResult();
    }

    @Override
    public String updateAddress(Address address) {
        Address temp = addressMapper.selectById(address.getId());
        if (temp == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        temp.setAddress(address.getAddress());
        temp.setReceiverName(address.getReceiverName());
        temp.setReceiverPhone(address.getReceiverPhone());
        temp.setUpdateTime(new Date());
        if (addressMapper.updateById(temp) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public List<Address> getAddressPage(Integer id) {
        return addressMapper.selectList(Wrappers.<Address>lambdaQuery().eq(Address::getUserId, id));
    }

    @Override
    public Address getAddressByUserid(Integer userid) {
        List<Address> adds = addressMapper.selectList(new QueryWrapper<Address>()
                .eq("user_id", userid));
        return adds.get(0);
    }
}